package com.shared;

import java.util.HashMap;
import java.util.Map;

/**
 * Enumeration defining the System messages.<p>
 * System messages are encoded by a special delimeter, which converted to ASCII
 * code 254, cannot be entered and specified by end users.
 * 
 * @author jiafanz
 */
public enum SystemMsg
{
    Connected,
    NoPair,
    Serviced,
    Disconnected;
    
    public static final int DELIMETER = 254;
    private static String dChar = new Character((char)DELIMETER).toString();

    
    // To boost performance store the values in a static local variable
    final private static SystemMsg[] VALUES = values();

    // Define a Map for mapping display names to the corresponding Enumerations.
    private static Map<String, String> displayNames = new HashMap<String, String>();
    static
    {
        displayNames.put(Connected.name(),          dChar + "Connected" + dChar);
        displayNames.put(NoPair.name(),             dChar + "NoPair" + dChar);
        displayNames.put(Serviced.name(),           dChar + "Serviced" + dChar);
        displayNames.put(Disconnected.name(),       dChar + "Disconnected" + dChar);
    }
    
    /**
     * Creates the correct Enumeration value given it's ordinal.
     * @param ordinal the ordinal of the Enumeration value  to create.
     * @return the Enumeration value corresponding to the specified ordinal.
     */
    static public SystemMsg getSystemMsg(int ordinal)
    {
        if (ordinal < 0 || ordinal >= VALUES.length)
        {
            return null;
        }
        return VALUES[ordinal];
    }
    
    /**
     * Return the appropriate Enumeration value for the specified name.
     * @param name the name of the value to return.
     * @return the required Enumeration value.
     */
    public static SystemMsg getSystemMsg(String name)
    {
        for (SystemMsg value : VALUES)
        {
            if (value.toString().equals(name))
            {
                return value;
            }
        }
        return null;
    }
    
    /**
     * Returns a display string representing this Enumeration.
     * @return a display string representing this Enumeration. 
     */
    final public String toString()
    {
        return displayNames.get(name());
    }
}
